<template>
  <!-- 综合价格分类 -->
  <div style="position: fixed; top: 54px; width: 100%; z-index: 12">
    <div class="nva">
      <span :class="isActive === index ? 'active' : ''" @click="sift(index)" v-for="(item, index) in navList" :key="index">{{
        item
      }}</span>
    </div>
  </div>
  <div v-show="haveGoods">
    <!-- 蒙版 -->
    <div class="mask" v-show="showMask" @click="handelmask"></div>
    <div style="height: 94px; width: 10px"></div>

    <!-- 价格筛选 -->
    <div class="m-priceSort" v-if="showPrice">
      <div class="form">
        <div class="formGroup">
          <span>筛选</span>
          <input v-model="maxPrice" placeholder="最高价" />
          <span>—</span>
          <input v-model="minPrice" placeholder="最低价" />
        </div>
        <div class="formbottom">
          <span>排序</span>
          <button>从高到低</button>
          <button>从低到高</button>
        </div>
      </div>
      <div class="btnGroup">
        <button @click="handelmask">取消</button>
        <button>确定</button>
      </div>
    </div>

    <!-- 分类 -->
    <div class="mulitiValsDropdown" v-if="showClassify">
      <ul class="list">
        <li
          :class="classifyHight === item.id ? 'active' : ''"
          v-for="item in categoryL1List"
          :key="item.id"
          @click="handelMuliti(item)"
        >
          {{ item.name }}
        </li>
      </ul>
    </div>

    <!-- 商品列表 -->
    <div class="goodsList">
      <VGoodsCard v-for="item in directlyList" :key="item.id" :goodItem="item" />
    </div>
  </div>
  <!-- 当前关键字没有商品时 -->
  <div class="noGoods" v-show="!haveGoods">
    <van-icon name="shop-collect" />
    <p>你寻找的商品还未上架</p>
  </div>
</template>

<script lang="ts" setup>
import VGoodsCard from './goodsCard.vue'
import { reqSearchGoods } from '@/api/search'
import { IdirectlyList, IcategoryL1List } from '@/types/search'
import { watch, ref } from 'vue'

//展示搜索结果为空页面
const haveGoods = ref(true)

//搜索结果商品列表
const directlyList = ref<IdirectlyList[]>([])
//搜索分类列表
const categoryL1List = ref<IcategoryL1List[]>([])
// 是否展示蒙版
const showMask = ref(false)
//控制导航栏字体颜色
const navList = ['综合', '价格', '分类']
const isActive = ref(0)

//价格筛选的最高和最低
const maxPrice = ref()
const minPrice = ref()
// 控制价格筛选框是否展示
const showPrice = ref(false)

//控制分类框是否展示
const showClassify = ref(false)
//控制分类框是否高亮
const classifyHight = ref(0)

//接受搜索组件传递的关键字
const props = defineProps({
  keyWord: { type: String }
})
//点击分类中的标签
const handelMuliti = (item: IcategoryL1List) => {
  showMask.value = false
  showClassify.value = false
  if (item.id === 0) {
    getSearchGoods(props.keyWord as string)
  } else {
    classifyHight.value = item.id
    getSearchGoods(item.name)
  }
}
//点击导航栏 综合  价格  分类  事件
const sift = (index: number) => {
  isActive.value = index
  if (index !== 0) {
    showMask.value = true
    if (index === 1) {
      showPrice.value = true
      showClassify.value = false
    } else {
      showClassify.value = true
      showPrice.value = false
    }
  } else {
    showMask.value = false
    showClassify.value = false
    showPrice.value = false
  }
}
//蒙版点击
const handelmask = () => {
  showMask.value = false
  showClassify.value = false
  showPrice.value = false
}

//根据关键字获取商品列表
const getSearchGoods = async (keyWord: string) => {
  const time = new Date().getTime()
  let res = await reqSearchGoods(keyWord, time)
  if (res.code === '200') {
    directlyList.value = res.data.directlyList
    categoryL1List.value = res.data.categoryL1List
    if (!(res.data.categoryL1List.length > 0)) {
      haveGoods.value = false
    } else {
      haveGoods.value = true
    }
  }
}

watch(
  props,
  () => {
    getSearchGoods(props.keyWord as string)
  },
  { immediate: true, deep: true }
)
</script>

<script lang="ts">
export default {
  name: 'VSearchList'
}
</script>

<style lang="scss" scoped>
.noGoods {
  position: absolute;
  top: 40%;
  left: 50%;
  .van-icon {
    font-size: 100px;
    color: #7f7f7f;
    position: relative;
    right: 35%;
  }
  p {
    position: relative;
    right: 50%;
    margin-top: 10px;
    font-size: 14px;
    color: #7f7f7f;
  }
}

.mulitiValsDropdown {
  box-sizing: border-box;
  width: 375px;
  background-color: white;
  position: fixed;
  top: 94px;
  padding: 20px 32.5px;
  z-index: 12;
  .list {
    .active {
      color: red;
      border: 1px solid red;
    }
    li {
      color: #333;
      font-size: 14px;
      text-align: center;
      border: 1px solid rgb(186, 181, 181);
      float: left;
      border-radius: 3px;
      padding: 5px 9px;
      margin: 0 20px 20px 0;
    }
  }
}
.m-priceSort {
  width: 375px;
  height: 168px;
  background-color: white;
  position: fixed;
  top: 94px;
  z-index: 12;
  .btnGroup {
    height: 52px;
    margin-top: 20px;
    border-top: 1px solid rgb(208, 204, 204);
    button {
      &:first-child {
        border-right: 1px solid rgb(208, 204, 204);
      }
      &:last-child {
        color: red;
      }
      background-color: white;
      font-size: 14px;
      width: 187.5px;
      height: 52px;
      border: none;
    }
  }
  .form {
    height: 96px;
    .formGroup {
      font-size: 14px;
      display: flex;
      justify-content: space-around;
      height: 28px;
      padding: 20px 15px 0;
      span {
        line-height: 24px;
      }
      input {
        text-align: center;
        width: 120px;
        height: 24px;
      }
    }
    .formbottom {
      font-size: 14px;
      display: flex;
      align-items: flex-start;
      height: 28px;
      padding: 20px 15px 0;
      span {
        margin: 5px;
      }
      button {
        &:last-child {
          color: red;
        }
        background-color: white;
        border: 1px solid rgb(208, 204, 204);
        margin-left: 12px;
        width: 104px;
        height: 28px;
      }
    }
  }
}
.nva {
  color: rgb(81, 79, 79);
  height: 40px;
  display: flex;
  justify-content: space-around;
  background-color: #fafafa;
  font-size: 14px;
  border-bottom: 1px solid rgb(208, 204, 204);
  span {
    height: 40px;
    line-height: 40px;
  }
  .active {
    color: red;
  }
}
.mask {
  background-color: rgba(78, 77, 75, 0.2);
  height: 700px;
  width: 500px;
  position: fixed;
  top: 54px;
  z-index: 10;
}
.goodsList {
  display: flex;
  flex-wrap: wrap;
  background-color: white;
  z-index: 1;
}
</style>
